package com.zhaokuner.springboot.refresh;

import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.endpoint.RefreshEndpoint;

import java.util.ArrayList;
import java.util.Collection;

/**
 * <p>
 * Description:刷新
 * <p>
 * Copyright: 2018-现在 北京掌通未来科技有限公司
 * <p>
 *
 * @author 赵希坤 zhaoxikun
 * @date 2020/7/25 17:47
 */
@Log4j2
@Data
public class RefreshScopeLaunch {
    @Autowired
    private RefreshEndpoint refreshEndpoint;
    @Autowired
    private RefreshAfterRunner refreshAfterRunner;


    public Collection<String> refresh(String message) {
        Collection<String> collection = new ArrayList<>(0);
        Exception error = null;
        long ll = System.currentTimeMillis();
        try {
            collection = refreshEndpoint.refresh();
        } catch (Exception e) {
            error = e;
            log.error("~~~~~~~~~~~~~~~~~~~~refresh error", e);
        }
        log.info("~~~~~~~~~~~~~~~~~~~~refresh {} {}", null == error, System.currentTimeMillis() - ll);
        try {
            refreshAfterRunner.run(null == error, message, error);
        } catch (Exception e) {
            log.error("~~~~~~~~~~~~~~~~~~~~refresh run error", e);
        }
        return collection;
    }
}
